package com.car.loan.project.xr.service;

import com.car.loan.project.xr.domain.OverdueAdvancesLog;
import com.car.loan.project.xr.domain.OverdueUrgentLog;
import com.car.loan.project.xr.util.bo.business.LoanAlbumBo;
import com.car.loan.project.xr.util.enums.LoanAlbumType;
import com.car.loan.project.xr.util.enums.after.AdvancesType;
import com.car.loan.project.xr.util.vo.after.OverdueAdvancesInfo;
import com.car.loan.project.xr.util.vo.after.OverdueAdvancesInfo.AdvancesInfo;
import com.car.loan.project.xr.util.vo.after.OverdueUrgentLogInfo.UrgentLog;
import java.time.LocalDateTime;

import com.car.loan.project.cm.domain.CmBank;
import com.car.loan.project.xr.domain.OverdueInfo;
import com.car.loan.project.xr.mapper.OverdueAdvancesLogMapper;
import com.car.loan.project.xr.mapper.OverdueInfoMapper;
import com.car.loan.project.xr.mapper.OverdueUrgentLogMapper;
import com.car.loan.project.xr.util.bo.business.CustomerAddBo;
import com.car.loan.project.xr.util.repayment.RepaymentFormulaInterface;
import com.car.loan.project.xr.util.repayment.RepaymentPlanParam;
import com.car.loan.project.xr.util.repayment.RepaymentPlanUtil;
import com.car.loan.project.xr.util.vo.SearchUrgentInfoVo.OverdueLog;
import com.google.common.collect.Lists;
import com.car.loan.project.xr.util.vo.SearchUrgentInfoVo.CarInfo;
import com.car.loan.project.xr.util.vo.SearchUrgentInfoVo.AdvancesOverdueInfo;
import com.google.common.collect.Maps;
import com.car.loan.project.xr.util.vo.SearchUrgentInfoVo.BusinessInfo;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.aspectj.lang.annotation.DataScope;
import com.car.loan.project.system.domain.SysUser;
import com.car.loan.project.xr.util.bo.business.LoanDeletePageParam;
import com.car.loan.project.xr.util.bo.business.LoanUrgentPageParam;
import com.car.loan.project.xr.util.vo.LoanDeleteVo;
import com.car.loan.project.xr.util.vo.LoanDeleteVo.OtherInfo;
import com.car.loan.project.xr.util.vo.SearchDeleteVo;

import com.car.loan.common.utils.qiniu.QiniuUtil;
import com.car.loan.project.xr.domain.LoanAlbum;
import com.car.loan.project.xr.domain.LoanInsurance;
import com.car.loan.project.xr.mapper.LoanAlbumMapper;
import com.car.loan.project.xr.mapper.LoanInsuranceMapper;

import com.car.loan.common.exception.loan.LoanParameterException;
import com.car.loan.common.utils.BeanUtils;
import com.car.loan.project.xr.domain.Loan;
import com.car.loan.project.xr.domain.LoanCar;
import com.car.loan.project.xr.domain.LoanDetail;
import com.car.loan.project.xr.domain.LoanExt;
import com.car.loan.project.xr.domain.LoanStatus;
import com.car.loan.project.xr.mapper.LoanCarMapper;
import com.car.loan.project.xr.mapper.LoanDetailMapper;
import com.car.loan.project.xr.mapper.LoanExtMapper;
import com.car.loan.project.xr.mapper.LoanMapper;
import com.car.loan.project.xr.mapper.LoanStatusMapper;
import com.car.loan.project.xr.service.impl.LoanServiceImpl;
import com.car.loan.project.xr.util.RedisDataUtil;
import com.car.loan.project.xr.util.vo.LoanDetailVo.LoanBaseInfoVo;
import com.car.loan.project.xr.util.vo.SearchBusinessVo;
import com.car.loan.project.xr.util.vo.SearchDetailVo;
import com.car.loan.project.xr.util.vo.SearchUrgentInfoVo;
import com.car.loan.project.xr.util.vo.SearchUrgentVo;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author huangding
 * @date 2020/2/16 16:15
 */
@Service
public class SearchService {

    @Autowired
    private LoanMapper loanMapper;


    @Autowired
    private LoanCarMapper loanCarMapper;

    @Autowired
    private LoanExtMapper loanExtMapper;

    @Autowired
    private LoanDetailMapper loanDetailMapper;

    @Autowired
    private LoanStatusMapper loanStatusMapper;

    @Autowired
    private RedisDataUtil redisDataUtil;

    @Autowired
    private LoanServiceImpl loanService;

    @Autowired
    private LoanInsuranceMapper loanInsuranceMapper;

    @Autowired
    private LoanAlbumMapper loanAlbumMapper;

    @Autowired
    private QiniuUtil qiniuUtil;

    @Autowired
    private OverdueInfoMapper overdueInfoMapper;

    @Autowired
    private OverdueAdvancesLogMapper overdueAdvancesLogMapper;

    @Autowired
    private OverdueUrgentLogMapper overdueUrgentLogMapper;


    public SearchBusinessVo getSearchBusinessInfo(Long loanId) {
        SearchBusinessVo searchBusinessVo = new SearchBusinessVo();
        Optional<Loan> optionalLoan
            = Optional.ofNullable(loanMapper.selectById(loanId));
        Loan loan = optionalLoan.orElseThrow(() -> new LoanParameterException("无该业务数据"));
        //获取数据源
        Optional<LoanCar> optionalLoanCar = loanCarMapper.selectByLoanId(loanId);
        LoanCar loanCar = optionalLoanCar
            .orElseThrow(() -> new LoanParameterException("业务数据车辆信息异常"));
        Optional<LoanExt> optionalLoanExt = loanExtMapper.selectByLoanId(loanId);
        LoanExt loanExt = optionalLoanExt
            .orElseThrow(() -> new LoanParameterException("业务数据扩展信息异常"));

        Optional<LoanDetail> optionalLoanDetail = loanDetailMapper.selectByLoanId(loanId);
        LoanDetail loanDetail = optionalLoanDetail
            .orElseThrow(() -> new LoanParameterException("业务数据详情信息异常"));

        //客户信息
        searchBusinessVo
            .setCustomerInfo(loanService.getCustomerAddBo(loan.getId(), loan.getCustomerId()));
        //基本信息
        LoanBaseInfoVo loanBaseInfoVo = loanService.getLoanBaseInfoVo(loan);
        loanBaseInfoVo.setFileNo(loanDetail.getFileNo());
        searchBusinessVo.setLoanBaseInfo(loanBaseInfoVo);

        //业务信息
        SearchBusinessVo.BusinessInfo businessInfo = new SearchBusinessVo.BusinessInfo();
        BeanUtils.copyBeanProp(businessInfo, loanDetail);
        businessInfo.setBankName(redisDataUtil.getBankName(loan.getBankId()));
        businessInfo.setInstallmentAmount(loanExt.getInstallmentAmount());
        businessInfo.setRate(loanExt.getRate());
        businessInfo.setFinancingAmount(loanExt.getFinancingAmount());
        businessInfo.setTerm(loanExt.getTerm());
        businessInfo.setCarSeries(loanExt.getCarName());
        businessInfo.setCarType(loanCar.getCarType());
        businessInfo.setCarNature(loanCar.getCarNature());
        businessInfo.setPlateType(loanCar.getPlateType());
        businessInfo.setRegistProvince(loanCar.getRegistProvince());
        businessInfo.setRegistCity(loanCar.getRegistCity());
        businessInfo.setSellAmount(loanExt.getSellAmount());
        businessInfo.setInstallmentAmount(loanExt.getInstallmentAmount());
        businessInfo.setRate(loanExt.getRate());
        businessInfo.setFinancingAmount(loanExt.getFinancingAmount());
        businessInfo.setTerm(loanExt.getTerm());
        searchBusinessVo.setBusinessInfo(businessInfo);

        //车辆信息
        SearchBusinessVo.CarInfo carInfo = new SearchBusinessVo.CarInfo();
        BeanUtils.copyBeanProp(carInfo, loanCar);
        searchBusinessVo.setCarInfo(carInfo);

        Optional<LoanInsurance> loanInsuranceOptional = loanInsuranceMapper.selectByLoanId(loanId);
        if (loanInsuranceOptional.isPresent()) {
            LoanInsurance loanInsurance = loanInsuranceOptional.get();
            List<LoanInsurance> insuranceCarInfos = searchBusinessVo.getInsuranceCarInfos();
            insuranceCarInfos.add(loanInsurance);
            searchBusinessVo.setInsuranceCarInfos(insuranceCarInfos);
        }
        //获取附件
        List<LoanAlbum> loanAlbums = loanAlbumMapper.selectListByLoanId(loanId);
        Map<Integer, List<LoanAlbumBo>> loanAlbumMap = loanAlbums.stream().map(l -> {
            LoanAlbumBo loanAlbumBo = new LoanAlbumBo();
            if (LoanAlbumType.PDF.type == l.getType() || LoanAlbumType.VIDEOS.type == l.getType()) {
                loanAlbumBo.setUrl(qiniuUtil.getAuthUrlOriginalHttp(l.getUrl()));
            } else {
                loanAlbumBo.setUrl(qiniuUtil.getAdminAuthUrl(l.getUrl()));
                loanAlbumBo.setThumbnailUrl(qiniuUtil.getUrlByThumbnail(l.getUrl()));
            }
            loanAlbumBo.setLoanId(l.getLoanId());
            loanAlbumBo.setType(l.getType());
            loanAlbumBo.setDbUrl(l.getUrl());
            return loanAlbumBo;
        }).collect(Collectors.groupingBy(LoanAlbumBo::getType));
        searchBusinessVo.setLoanAlbumMap(loanAlbumMap);
        return searchBusinessVo;
    }


    public SearchDetailVo getSearchDetailInfo(Long loanId) {
        SearchDetailVo searchDetailVo = new SearchDetailVo();

        Optional<Loan> optionalLoan
            = Optional.ofNullable(loanMapper.selectById(loanId));
        Loan loan = optionalLoan.orElseThrow(() -> new LoanParameterException("无该业务数据"));
        //获取数据源
        Optional<LoanCar> optionalLoanCar = loanCarMapper.selectByLoanId(loanId);
        LoanCar loanCar = optionalLoanCar
            .orElseThrow(() -> new LoanParameterException("业务数据车辆信息异常"));
        Optional<LoanExt> optionalLoanExt = loanExtMapper.selectByLoanId(loanId);
        LoanExt loanExt = optionalLoanExt
            .orElseThrow(() -> new LoanParameterException("业务数据扩展信息异常"));
        Optional<LoanStatus> optionalLoanStatus = loanStatusMapper.selectByLoanId(loanId);
        LoanStatus loanStatus = optionalLoanStatus
            .orElseThrow(() -> new LoanParameterException("业务数据状态信息异常"));

        Optional<LoanDetail> optionalLoanDetail = loanDetailMapper.selectByLoanId(loanId);
        LoanDetail loanDetail = optionalLoanDetail
            .orElseThrow(() -> new LoanParameterException("业务数据详情信息异常"));

        //客户信息
        searchDetailVo
            .setCustomerInfo(loanService.getCustomerAddBo(loan.getId(), loan.getCustomerId()));
        //基本信息
        LoanBaseInfoVo loanBaseInfoVo = loanService.getLoanBaseInfoVo(loan);
        loanBaseInfoVo.setFileNo(loanDetail.getFileNo());
        searchDetailVo.setLoanBaseInfo(loanBaseInfoVo);

        //业务信息
        SearchBusinessVo.BusinessInfo businessInfo = new SearchBusinessVo.BusinessInfo();
        BeanUtils.copyBeanProp(businessInfo, loanDetail);
        businessInfo.setBankName(redisDataUtil.getBankName(loan.getBankId()));
        businessInfo.setInstallmentAmount(loanExt.getInstallmentAmount());
        businessInfo.setRate(loanExt.getRate());
        businessInfo.setFinancingAmount(loanExt.getFinancingAmount());
        businessInfo.setTerm(loanExt.getTerm());
        businessInfo.setCarSeries(loanCar.getType());
        businessInfo.setCarType(loanCar.getCarType());
        businessInfo.setCarNature(loanCar.getCarNature());
        businessInfo.setPlateType(loanCar.getPlateType());
        businessInfo.setRegistProvince(loanCar.getRegistProvince());
        businessInfo.setRegistCity(loanCar.getRegistCity());
        businessInfo.setSellAmount(loanExt.getSellAmount());
        businessInfo.setInstallmentAmount(loanExt.getInstallmentAmount());
        businessInfo.setRate(loanExt.getRate());
        businessInfo.setFinancingAmount(loanExt.getFinancingAmount());
        businessInfo.setTerm(loanExt.getTerm());
        searchDetailVo.setBusinessInfo(businessInfo);

        //车辆信息
        SearchBusinessVo.CarInfo carInfo = new SearchBusinessVo.CarInfo();
        BeanUtils.copyBeanProp(carInfo, loanCar);
        searchDetailVo.setCarInfo(carInfo);

        Optional<LoanInsurance> loanInsuranceOptional = loanInsuranceMapper.selectByLoanId(loanId);
        if (loanInsuranceOptional.isPresent()) {
            LoanInsurance loanInsurance = loanInsuranceOptional.get();
            List<LoanInsurance> insuranceCarInfos = searchDetailVo.getInsuranceCarInfos();
            insuranceCarInfos.add(loanInsurance);
            searchDetailVo.setInsuranceCarInfos(insuranceCarInfos);
        }
        //获取附件
        List<LoanAlbum> loanAlbums = loanAlbumMapper.selectListByLoanId(loanId);
        Map<Integer, List<LoanAlbumBo>> loanAlbumMap = loanAlbums.stream().map(l -> {
            LoanAlbumBo loanAlbumBo = new LoanAlbumBo();
            if (LoanAlbumType.PDF.type == l.getType() || LoanAlbumType.VIDEOS.type == l.getType()) {
                loanAlbumBo.setUrl(qiniuUtil.getAuthUrlOriginalHttp(l.getUrl()));
            } else {
                loanAlbumBo.setUrl(qiniuUtil.getAdminAuthUrl(l.getUrl()));
                loanAlbumBo.setThumbnailUrl(qiniuUtil.getUrlByThumbnail(l.getUrl()));
            }
            loanAlbumBo.setLoanId(l.getLoanId());
            loanAlbumBo.setType(l.getType());
            loanAlbumBo.setDbUrl(l.getUrl());
            return loanAlbumBo;
        }).collect(Collectors.groupingBy(LoanAlbumBo::getType));
        searchDetailVo.setLoanAlbumMap(loanAlbumMap);

        //财务信息
        SearchDetailVo.FinanceInfo financeInfo = new SearchDetailVo.FinanceInfo();
        BeanUtils.copyBeanProp(financeInfo, loanDetail);
        financeInfo.setSettleDate(loanExt.getSettleDate());
        financeInfo.setSalesmanRemark(loanDetail.getSalesmanRemark());
        financeInfo.setSettleRemark(loanExt.getSettleRemark());
        searchDetailVo.setFinanceInfo(financeInfo);

        //操作记录
        Map<String, SysUser> redisUserMap = redisDataUtil.getRedisUserMap();
        SearchDetailVo.OperationalInfo operationalInfo = new SearchDetailVo.OperationalInfo();
        BeanUtils.copyBeanProp(operationalInfo, loanStatus);
        operationalInfo
            .setBusinessName(getAdminUserName(loanStatus.getBusinessAdminId(), redisUserMap));
        operationalInfo
            .setBusinessAuditName(getAdminUserName(loanStatus.getBusinessAuditId(), redisUserMap));
        operationalInfo.setBusinessApostilleName(
            getAdminUserName(loanStatus.getBusinessApostilleId(), redisUserMap));
        operationalInfo
            .setPaymentApplyName(getAdminUserName(loanStatus.getPaymentApplyId(), redisUserMap));
        operationalInfo
            .setPaymentAuditName(getAdminUserName(loanStatus.getPaymentAuditId(), redisUserMap));
        operationalInfo
            .setPaymentInfoName(getAdminUserName(loanStatus.getPaymentInfoId(), redisUserMap));
        operationalInfo.setSettleAccountsName(
            getAdminUserName(loanStatus.getSettleAccountsId(), redisUserMap));
        operationalInfo
            .setCarInfoName(getAdminUserName(loanStatus.getCarInfoId(), redisUserMap));
        operationalInfo
            .setInsuranceInfoName(getAdminUserName(loanStatus.getInsuranceInfoId(), redisUserMap));
        operationalInfo
            .setLicenseInfoName(getAdminUserName(loanStatus.getLicenseInfoId(), redisUserMap));
        operationalInfo
            .setMortgageInfoName(getAdminUserName(loanStatus.getMortgageInfoId(), redisUserMap));
        operationalInfo
            .setContractAuditName(getAdminUserName(loanStatus.getContractAuditId(), redisUserMap));
        operationalInfo
            .setMakeLoansInfoName(getAdminUserName(loanStatus.getMakeLoansInfoId(), redisUserMap));
        operationalInfo
            .setRepaymentInfoName(getAdminUserName(loanStatus.getRepaymentInfoId(), redisUserMap));
        operationalInfo
            .setFileAuditName(getAdminUserName(loanStatus.getFileAuditId(), redisUserMap));
        operationalInfo.setRemark(loanDetail.getRemark());
        operationalInfo.setAuditRemark(loanDetail.getAuditRemark());
        operationalInfo.setSecondAuditRemark(loanDetail.getSecondAuditRemark());
        searchDetailVo.setOperationalInfo(operationalInfo);
        return searchDetailVo;
    }

    private String getAdminUserName(Long userId, Map<String, SysUser> redisUserMap) {
        if (null == userId) {
            return StringUtils.EMPTY;
        }
        SysUser sysUser = redisUserMap.get(userId.toString());
        if (null == sysUser) {
            return StringUtils.EMPTY;
        }
        return sysUser.getUserName();
    }

    public LoanDeleteVo getSearchDeleteInfo(Long loanId) {
        Optional<Loan> optionalLoan
            = Optional.ofNullable(loanMapper.selectById(loanId));
        Loan loan = optionalLoan.orElseThrow(() -> new LoanParameterException("无该业务数据"));
        //获取数据源
        Optional<LoanCar> optionalLoanCar = loanCarMapper.selectByLoanId(loanId);
        LoanCar loanCar = optionalLoanCar
            .orElseThrow(() -> new LoanParameterException("业务数据车辆信息异常"));
        Optional<LoanExt> optionalLoanExt = loanExtMapper.selectByLoanId(loanId);
        LoanExt loanExt = optionalLoanExt
            .orElseThrow(() -> new LoanParameterException("业务数据扩展信息异常"));
        Optional<LoanDetail> optionalLoanDetail = loanDetailMapper.selectByLoanId(loanId);
        LoanDetail loanDetail = optionalLoanDetail
            .orElseThrow(() -> new LoanParameterException("业务数据详情信息异常"));

        LoanDeleteVo loanDeleteVo = new LoanDeleteVo();
        //客户信息
        loanDeleteVo
            .setCustomerInfo(loanService.getCustomerAddBo(loan.getId(), loan.getCustomerId()));
        //基本信息
        LoanBaseInfoVo loanBaseInfoVo = loanService.getLoanBaseInfoVo(loan);
        loanBaseInfoVo.setFileNo(loanDetail.getFileNo());
        loanDeleteVo.setLoanBaseInfo(loanBaseInfoVo);
        //业务信息
        LoanDeleteVo.BusinessInfoVo businessInfo = new LoanDeleteVo.BusinessInfoVo();
        businessInfo.setBankName(redisDataUtil.getBankName(loan.getBankId()));
        businessInfo.setInstallmentAmount(loanExt.getInstallmentAmount());
        businessInfo.setRate(loanExt.getRate());
        businessInfo.setCarName(loanExt.getCarName());
        businessInfo.setTerm(loanExt.getTerm());
        businessInfo.setCarType(loanCar.getCarType());
        businessInfo.setCarNature(loanCar.getCarNature());
        businessInfo.setPlateType(loanCar.getPlateType());
        businessInfo.setRegistProvince(loanCar.getRegistProvince());
        businessInfo.setRegistCity(loanCar.getRegistCity());
        businessInfo.setSellAmount(loanExt.getSellAmount());
        businessInfo.setInstallmentAmount(loanExt.getInstallmentAmount());
        businessInfo.setRate(loanExt.getRate());
        businessInfo.setTerm(loanExt.getTerm());
        loanDeleteVo.setBusinessInfo(businessInfo);
        //其他信息
        LoanDeleteVo.OtherInfo otherInfo = new LoanDeleteVo.OtherInfo();
        otherInfo.setDeletedTime(loan.getDeletedTime());
        otherInfo.setDeletedName(redisDataUtil.getAdminName(loan.getDeletedId()));
        otherInfo.setBusinessSuggest(loanDetail.getBusinessSuggest());
        otherInfo.setRemark(loanDetail.getRemark());
        otherInfo.setAuditRemark(loanDetail.getAuditRemark());
        otherInfo.setSecondAuditRemark(loanDetail.getSecondAuditRemark());
        loanDeleteVo.setOtherInfo(otherInfo);
        //获取附件
        List<LoanAlbum> loanAlbums = loanAlbumMapper.selectListByLoanId(loanId);
        Map<Integer, List<LoanAlbumBo>> loanAlbumMap = loanAlbums.stream().map(l -> {
            LoanAlbumBo loanAlbumBo = new LoanAlbumBo();
            if (LoanAlbumType.PDF.type == l.getType() || LoanAlbumType.VIDEOS.type == l.getType()) {
                loanAlbumBo.setUrl(qiniuUtil.getAuthUrlOriginalHttp(l.getUrl()));
            } else {
                loanAlbumBo.setUrl(qiniuUtil.getAdminAuthUrl(l.getUrl()));
                loanAlbumBo.setThumbnailUrl(qiniuUtil.getUrlByThumbnail(l.getUrl()));
            }
            loanAlbumBo.setLoanId(l.getLoanId());
            loanAlbumBo.setType(l.getType());
            loanAlbumBo.setDbUrl(l.getUrl());
            return loanAlbumBo;
        }).collect(Collectors.groupingBy(LoanAlbumBo::getType));
        loanDeleteVo.setLoanAlbumMap(loanAlbumMap);
        return loanDeleteVo;
    }

    @DataScope(deptAlias = "d", userAlias = "u")
    public Page<SearchDeleteVo> deletePage(LoanDeletePageParam loanDeletePageParam) {
        Page page = new Page(loanDeletePageParam.getPageNum(), loanDeletePageParam.getPageSize());
        return loanMapper.findLoanDeletePage(page, loanDeletePageParam);
    }

    @DataScope(deptAlias = "d", userAlias = "u")
    public Page<SearchUrgentVo> urgentPage(LoanUrgentPageParam loanUrgentPageParam) {
        Page page = new Page(loanUrgentPageParam.getPageNum(), loanUrgentPageParam.getPageSize());
        return loanMapper.findLoanUrgentPage(page, loanUrgentPageParam);
    }

    public SearchUrgentInfoVo getSearchUrgentInfo(Long loanId) {
        SearchUrgentInfoVo searchUrgentInfoVo = new SearchUrgentInfoVo();

        Optional<Loan> optionalLoan
            = Optional.ofNullable(loanMapper.selectById(loanId));
        Loan loan = optionalLoan.orElseThrow(() -> new LoanParameterException("无该业务数据"));
        //获取数据源
        Optional<LoanCar> optionalLoanCar = loanCarMapper.selectByLoanId(loanId);
        LoanCar loanCar = optionalLoanCar
            .orElseThrow(() -> new LoanParameterException("业务数据车辆信息异常"));
        Optional<LoanExt> optionalLoanExt = loanExtMapper.selectByLoanId(loanId);
        LoanExt loanExt = optionalLoanExt
            .orElseThrow(() -> new LoanParameterException("业务数据扩展信息异常"));
        Optional<LoanDetail> optionalLoanDetail = loanDetailMapper.selectByLoanId(loanId);
        LoanDetail loanDetail = optionalLoanDetail
            .orElseThrow(() -> new LoanParameterException("业务数据详情信息异常"));

        //客户信息
        searchUrgentInfoVo
            .setCustomerInfo(loanService.getCustomerAddBo(loan.getId(), loan.getCustomerId()));
        //基本信息
        LoanBaseInfoVo loanBaseInfoVo = loanService.getLoanBaseInfoVo(loan);
        loanBaseInfoVo.setFileNo(loanDetail.getFileNo());
        searchUrgentInfoVo.setLoanBaseInfo(loanBaseInfoVo);

        //业务信息
        SearchUrgentInfoVo.BusinessInfo businessInfo = new SearchUrgentInfoVo.BusinessInfo();
        businessInfo.setBankName(redisDataUtil.getBankName(loan.getBankId()));
        businessInfo.setLendDate(loanDetail.getLendDate());
        businessInfo.setSellAmount(loanExt.getSellAmount());
        businessInfo.setInstallmentAmount(loanExt.getInstallmentAmount());
        businessInfo.setRate(loanExt.getRate());
        businessInfo.setTerm(loanExt.getTerm());
        businessInfo.setFinancingAmount(loanExt.getFinancingAmount());
        businessInfo.setRepayCardNo(loanDetail.getRepayCardNo());
        businessInfo.setDeductionCardNo(loanDetail.getDeductionCardNo());
        searchUrgentInfoVo.setBusinessInfo(businessInfo);

        //车辆信息
        CarInfo carInfo = new CarInfo();
        carInfo.setBankCarBrand(loanCar.getBankCarBrand());
        carInfo.setBankCarType(loanCar.getBankCarType());
        carInfo.setFrameNo(loanCar.getFrameNo());
        carInfo.setEngineNo(loanCar.getEngineNo());
        carInfo.setInvoiceUnit(loanCar.getInvoiceUnit());
        carInfo.setInvoiceDate(loanCar.getInvoiceDate());
        carInfo.setInvoicePrice(loanCar.getInvoicePrice());
        carInfo.setRegistOwner(loanCar.getRegistOwner());
        carInfo.setFirstRegistDate(loanCar.getFirstRegistDate());
        carInfo.setRegistDate(loanCar.getRegistDate());
        carInfo.setColor(loanCar.getColor());
        searchUrgentInfoVo.setCarInfo(carInfo);

        //逾期垫款信息
        AdvancesOverdueInfo advancesOverdueInfo = new AdvancesOverdueInfo();
        advancesOverdueInfo.setOverdueAmount(loanExt.getOverdueAmount());
        advancesOverdueInfo.setAdvancesAmount(loanExt.getAdvancesAmount());
        advancesOverdueInfo.setOverdueMaxAmount(loanExt.getOverdueMaxAmount());
        advancesOverdueInfo.setOverdueCount(loanExt.getOverdueCount());
        advancesOverdueInfo.setCurrentTerm(RepaymentPlanUtil
            .getCurrentRepayTerm(LocalDate.now(), LocalDate.parse(loanDetail.getFirstRepayDate()),
                loanExt.getTerm()));
        searchUrgentInfoVo.setAdvancesOverdueInfo(advancesOverdueInfo);

        //还款信息

        CmBank cmBank = redisDataUtil.getRedisBankMap().get(loan.getBankId().toString());
        //还款计划获取
        RepaymentFormulaInterface repaymentFormulaInterface = new RepaymentFormulaInterface() {
        };
        RepaymentPlanParam repaymentPlanParam = new RepaymentPlanParam();
        repaymentPlanParam.setFinancingAmount(new BigDecimal(loanExt.getFinancingAmount()));
        repaymentPlanParam.setFirstStatementDate(loanDetail.getFirstRepayDate());
        repaymentPlanParam.setGracePeriod(cmBank.getGracePeriod());
        repaymentPlanParam.setTerm(Integer.parseInt(loanExt.getTerm().toString()));
        repaymentPlanParam.setMonthRepayInterest(loanDetail.getMonthRepayInterest());
        repaymentPlanParam.setMonthRepayPrincipal(loanDetail.getMonthRepayPrincipal());
        repaymentPlanParam.setFirstRepayInterest(loanDetail.getFirstRepayInterest());
        repaymentPlanParam.setFirstRepayPrincipal(loanDetail.getFirstRepayPrincipal());
        repaymentPlanParam.setLastRepayInterest(loanDetail.getLastRepayInterest());
        repaymentPlanParam.setLastRepayPrincipal(loanDetail.getLastRepayPrincipal());
        searchUrgentInfoVo
            .setRepaymentPlans(repaymentFormulaInterface.getRepaymentPlans(repaymentPlanParam));
        //获取附件
        List<LoanAlbum> loanAlbums = loanAlbumMapper.selectListByLoanId(loanId);
        Map<Integer, List<LoanAlbumBo>> loanAlbumMap = loanAlbums.stream().map(l -> {
            LoanAlbumBo loanAlbumBo = new LoanAlbumBo();
            if (LoanAlbumType.PDF.type == l.getType() || LoanAlbumType.VIDEOS.type == l.getType()) {
                loanAlbumBo.setUrl(qiniuUtil.getAuthUrlOriginalHttp(l.getUrl()));
            } else {
                loanAlbumBo.setUrl(qiniuUtil.getAdminAuthUrl(l.getUrl()));
                loanAlbumBo.setThumbnailUrl(qiniuUtil.getUrlByThumbnail(l.getUrl()));
            }
            loanAlbumBo.setLoanId(l.getLoanId());
            loanAlbumBo.setType(l.getType());
            loanAlbumBo.setDbUrl(l.getUrl());
            return loanAlbumBo;
        }).collect(Collectors.groupingBy(LoanAlbumBo::getType));
        searchUrgentInfoVo.setLoanAlbumMap(loanAlbumMap);

        //逾期记录
        List<OverdueInfo> overdueInfos = overdueInfoMapper.selectByLoanId(loanId);
        if (null != overdueInfos && overdueInfos.size() > 0) {
            List<OverdueLog> logList = new ArrayList<>(overdueInfos.size());
            for (OverdueInfo overdueInfo : overdueInfos) {
                OverdueLog overdueLog = new OverdueLog();
                overdueLog.setTerm(overdueInfo.getItem());
                overdueLog.setImportTime(overdueInfo.getUpdateTime());
                overdueLog.setDataDate(overdueInfo.getDataDate());
                overdueLog.setPayableAmount(overdueInfo.getPayableAmount());
                overdueLog.setOverdueAmount(overdueInfo.getOverdueAmount());
                overdueLog.setMaxOverdueAmount(overdueInfo.getMaxOverdueAmount());
                logList.add(overdueLog);
            }
            searchUrgentInfoVo.setOverdueLogs(logList);
        }

        //垫款记录'
        List<OverdueAdvancesLog> overdueAdvancesLogs = overdueAdvancesLogMapper
            .selectByLoanId(loanId);
        if (null != overdueAdvancesLogs && overdueAdvancesLogs.size() > 0) {
            List<AdvancesInfo> advancesInfos = new ArrayList<>(overdueAdvancesLogs.size());
            for (OverdueAdvancesLog overdueAdvancesLog : overdueAdvancesLogs) {
                OverdueAdvancesInfo.AdvancesInfo advancesInfo = new OverdueAdvancesInfo.AdvancesInfo();
                advancesInfo.setDateTime(overdueAdvancesLog.getCreateTime());
                Integer type = overdueAdvancesLog.getType();
                if (AdvancesType.EXPENSES.type == type) {
                    advancesInfo.setExpensesAmount(overdueAdvancesLog.getAmount());
                } else if (AdvancesType.INCOME.type == type) {
                    advancesInfo.setIncomeAmount(overdueAdvancesLog.getAmount());
                }
                advancesInfo.setAdvancesBalance(overdueAdvancesLog.getBalance());
                advancesInfo.setRemark(overdueAdvancesLog.getRemark());
                advancesInfos.add(advancesInfo);
            }
            searchUrgentInfoVo.setAdvancesInfos(advancesInfos);
        }
        Map<String, SysUser> redisUserMap = redisDataUtil.getRedisUserMap();
        //催收记录
        List<OverdueUrgentLog> overdueUrgentLogs = overdueUrgentLogMapper.selectByLoanId(loanId);
        if (null != overdueUrgentLogs && overdueUrgentLogs.size() > 0) {
            List<UrgentLog> urgentLogs = new ArrayList<>(overdueUrgentLogs.size());
            for (OverdueUrgentLog overdueUrgentLog : overdueUrgentLogs) {
                UrgentLog urgentLog = new UrgentLog();
                urgentLog.setType(overdueUrgentLog.getType());
                urgentLog.setName(overdueUrgentLog.getName());
                urgentLog.setMobile(overdueUrgentLog.getMobile());
                urgentLog.setRelationship(overdueUrgentLog.getRelationship());
                urgentLog.setRemark(overdueUrgentLog.getRemark());
                SysUser sysUser = redisUserMap.get(overdueUrgentLog.getCreatorId().toString());
                if (null != sysUser) {
                    urgentLog.setAdminName(sysUser.getUserName());
                }
                urgentLog.setCreateTime(overdueUrgentLog.getCreateTime());
                urgentLogs.add(urgentLog);
            }
            searchUrgentInfoVo.setUrgentLogs(urgentLogs);
        }
        return searchUrgentInfoVo;
    }
}
